package com.atguigu.msmservice.service.impl;

import com.atguigu.msmservice.service.MsmService;
import com.atguigu.msmservice.utils.PropertiesUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    //邮箱发送验证码
    @Override
    public boolean send(Map<String, Object> param, String email)  {
        if(StringUtils.isEmpty(email)) return false;

        try {
            HtmlEmail htmlEmail =new HtmlEmail();//创建一个HtmlEmail实例对象
            //设置邮箱的SMTP服务器，qq邮箱为smtp.qq.com
            htmlEmail.setHostName(PropertiesUtil.HOSTNAME);
            //设置发送的字符类型
            htmlEmail.setCharset("utf-8");
            //设置收件人
            htmlEmail.addTo(email);
            //发送人的邮箱为自己的，用户名可以随便填
            htmlEmail.setFrom(PropertiesUtil.EMALI, (String) param.get("name"));
            //设置发送人到的邮箱和用户名和授权码(授权码是自己设置的)
            htmlEmail.setAuthentication(PropertiesUtil.EMALI,PropertiesUtil.PASSWORD);
            //设置发送主题
            htmlEmail.setSubject((String) param.get("subject"));
            //设置发送内容
            htmlEmail.setMsg((String) param.get("msg"));
            //进行发送
            htmlEmail.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }

        return true;
    }
}
